package com.example.finalsystem.Controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.example.finalsystem.Entity.Deal;
import com.example.finalsystem.Entity.ListGoods;
import com.example.finalsystem.Entity.User;
import com.example.finalsystem.Service.DealService;
import com.example.finalsystem.Service.ListGoodsService;
import com.example.finalsystem.Service.MemberService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.List;

@RestController
public class MemberController {
    @Autowired
    MemberService memberService;
    @Autowired
    DealService dealService;
    @Autowired
    ListGoodsService listGoodsService;
    private SqlSession openSession()
    {
        String resource = "mybatis-config.xml";
        InputStream is = MemberController.class.getClassLoader().getResourceAsStream(resource);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        return   sessionFactory.openSession(true);
    }

    @ResponseBody
    @PostMapping("/member/getSale")
    public String getSale(@RequestParam String userId)
    {
        SqlSession sqlSession = openSession();
        System.out.println(userId);
        List<Deal> deals = dealService.findDealByStaffId(userId,sqlSession);
        double sale = 0;
        for (Deal i:deals
             ) {
            String id = i.getDealid();
            for (ListGoods l: listGoodsService.getListGoodsByDealId(id,sqlSession)
                 ) {

                sale+= l.getGoodsprice()*l.getGoodsnumber();
                System.out.println(sale);
            }
        }
        sqlSession.close();
        return sale+"";
    }



    @ResponseBody
    @PostMapping("/member/addPerson")
    public String addPerson(@RequestParam String userId,@RequestParam String userName,@RequestParam String password){
        return memberService.addPerson(userId,userName,password);
    }
    @ResponseBody
    @PostMapping("/member/fixPerson")
    public String fixPerson(@RequestParam String userId,@RequestParam String userName,@RequestParam String password,@RequestParam String role){
        return memberService.fixPerson(userId,userName,password,role);
    }
    @ResponseBody
    @PostMapping("/member/deletePerson")
    public String deletePerson(@RequestParam String userId){
        return memberService.deletePerson(userId);
    }
//    获取人员总数
    @ResponseBody
    @PostMapping("/member/countMember")
    public String countMember()
    {
        return memberService.countMember()+"";
    }
// 参数要求 1-n 页  size 大小 1-n行
    @ResponseBody
    @PostMapping("/member/getListOfUser")
    public String getListOfUser(@RequestParam int currentPage,@RequestParam int pageSize)
    {
SqlSession sqlSession = openSession();
        List<User>  users=memberService.getListOfUser(currentPage,pageSize,sqlSession);
        sqlSession.close();
        return JSONArray.toJSONString(users);
    }
    @ResponseBody
    @PostMapping("/member/getUser")
    public String getUser(@RequestParam String userId){
        return JSON.toJSONString(memberService.getPerson(userId));
    }
    @ResponseBody
    @PostMapping("/member/getUserId")
    public String getUser1(HttpServletRequest request){
        User user = (User)request.getSession().getAttribute("user");
        return user.getUserid();
    }
    @ResponseBody
    @PostMapping("/member/getUserRole")
    public String getUserRole(HttpServletRequest request){
        User user = (User)request.getSession().getAttribute("user");
        return user.getUserrole();
    }
    @PostMapping("/member/getUserName")
    public String getUserName(HttpServletRequest request){
        User user = (User)request.getSession().getAttribute("user");
        return user.getUsername();
    }
}
